package com.itbaizhan.controller;

import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.request.RequestScope;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.View;

import java.io.IOException;
import java.util.Map;

@Controller
public class MyController2 {

    @RequestMapping("/hello")
    public void hello(){
        System.out.println("hello mvc");
    }

    @RequestMapping("/c2/hello")
    public String hello1(){
        System.out.println("hello mvc");
        return "hello";
    }

    @RequestMapping("/c2/hello2")
    public ModelAndView hello2(){
        ModelAndView modelAndView = new ModelAndView();
        Map<String, Object> model = modelAndView.getModel();
        model.put("name","xujiale");
        modelAndView.setViewName("baizhan");
        return modelAndView;
    }

    @RequestMapping("/c2/hello3")
    public String hello3(HttpSession session){
        session.setAttribute("name","xujiale");
        ServletContext context = session.getServletContext();
        context.setAttribute("name","cuixuefei");
        return "baizhan";
    }

    @RequestMapping("/c2/hello4")
    public void hello4(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setAttribute("name","xujiale");
//        request.getRequestDispatcher("/c2/hello5").forward(request,response);
        response.sendRedirect("/c2/hello5");

    }

    @RequestMapping("/c2/hello5")
    public void hello5(HttpServletRequest request){
        System.out.println("receive the request");
        System.out.println(request.getAttribute("name"));
    }

    @RequestMapping("/c2/hello6")
    public String hello6(HttpServletRequest request){
        request.setAttribute("name","xujiale");
        return "forward:/c2/hello5";
    }
}
